package handler

import (
	"api-gate/pkg"
	__ "api-gate/proto"
	"api-gate/request"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
	"path/filepath"
	"time"
)

func GoodsAdd(c *gin.Context) {
	var req request.GoodsAdd
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:9999", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	s := __.NewGoodsClient(conn)

	// Contact the server and print out its response.
	_, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	add, err := s.GoodsAdd(c, &__.GoodsAddRequest{
		GoodsNo:     req.GoodsNo,
		GoodsName:   req.GoodsName,
		GoodsText:   req.GoodsText,
		GoodsPrice:  float32(req.GoodsPrice),
		GoodsNum:    int64(req.GoodsNum),
		GoodsImg:    req.GoodsImg,
		GoodsType:   req.GoodsType,
		GoodsStatus: req.GoodsStatus,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": add,
	})
	return
}

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)
	fi := filepath.Ext(file.Filename)
	name := time.Now().Format(time.DateTime) + fi
	dst := "./img/upload/" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)
	pkg.OssUpload(name, dst)
	c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
}

func GoodsItemAdd(c *gin.Context) {
	var req request.GoodsItemAdd
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:9999", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	s := __.NewGoodsClient(conn)

	// Contact the server and print out its response.
	_, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	add, err := s.GoodsItemAdd(c, &__.GoodsItemAddRequest{
		GoodsId:    int64(req.GoodsId),
		LunchType:  req.LunchType,
		ItemNo:     req.ItemNo,
		Colour:     req.Colour,
		Place:      req.Place,
		UserPerson: req.UserPerson,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": add,
	})
	return
}

func GoodsOther(c *gin.Context) {
	var req request.GoodsOther
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:9999", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	s := __.NewGoodsClient(conn)

	// Contact the server and print out its response.
	_, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	other, err := s.GoodsOther(c, &__.GoodsOtherAddRequest{
		GoodsId:     int64(req.GoodsId),
		GoodsItemId: int64(req.GoodsItemId),
		Server:      req.Server,
		Status:      req.Status,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": other,
	})
	return
}

func GoodsList(c *gin.Context) {
	var req request.GoodsList
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:9999", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	s := __.NewGoodsClient(conn)

	// Contact the server and print out its response.
	_, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	list, err := s.GoodsList(c, &__.GoodsListRequest{
		Page:      req.Page,
		Size:      req.Size,
		GoodsName: req.GoodsName,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": list,
	})
	return
}
